<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
            v-model="queryParams.model"
            placeholder="请输入型号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="wareinList" >
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="颜色" align="center" prop="color" />
      <!--
      <el-table-column label="规格" align="center" prop="gg" /> -->
      <el-table-column label="颜色" align="center" prop="wcolor" />
      <el-table-column label="规格名称" align="center" prop="ggName" />
      <el-table-column label="供货价格" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="添加时间" align="center" prop="addtime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.addtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="upTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.upTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />



  </div>
</template>

<script setup name="Warein">
import { listWarein, getWarein, delWarein, addWarein, updateWarein } from "@/api/mxerp/warein";

const { proxy } = getCurrentInstance();

const wareinList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    productId: null,
    wareId: null,
    num: null,
    userId: null,
    addtime: null,
    upTime: null,
    name: null,
    model: null,
    color: null,
    gg: null,
    wcolor: null,
    ggName: null,
    price: null
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询入库记录列表 */
function getList() {
  loading.value = true;
  listWarein(queryParams.value).then(response => {
    wareinList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    productId: null,
    wareId: null,
    num: null,
    userId: null,
    addtime: null,
    upTime: null,
    remark: null,
    name: null,
    model: null,
    color: null,
    gg: null,
    wcolor: null,
    ggName: null,
    price: null
  };
  proxy.resetForm("wareinRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加入库记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWarein(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改入库记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["wareinRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarein(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarein(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除入库记录编号为"' + _ids + '"的数据项？').then(function() {
    return delWarein(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mxerp/warein/export', {
    ...queryParams.value
  }, `warein_${new Date().getTime()}.xlsx`)
}

getList();
</script>
